var uid = getParameter("uid");
var month = getParameter("month");
var now = new Date();
var maxdate = 30;
month=12;
$(function() {
	//now.setDate(0);
	now.setYear(2015);
	now.setMonth(month-1, 1);
	if(month==2){
		maxdate = 28;
	}else if(month == 4 || month==6 || month==9 || month==11){
		maxdate = 29;
	}
	$('#chart_title').html(month+"月血糖报告");
	charts(now);
});

function charts(now) {
	var urlStr = serverPath + "/app/doctor/diabetes/sugarView.do";
	
	var jsonStr = 
	    '{'+
	    '	   "requestid":"",'+
	    '	   "requestsource":"diabetes",'+
	    '	   "sessionid":"",'+
	    '	   "uid":"'+uid+'",'+
	    '	   "method":"showSugarReport",'+
	    '	   "version":"1.0",'+
	    '	   "requestlist":'+
	    '	   ['+
	    '	      {'+
	    '			 "befdate":"'+getLastDay(now, 0).format("yyyy-MM-dd")+'",'+
	    '			 "aftdate":"'+getNextDay(now, maxdate).format("yyyy-MM-dd")+'",'+
	    '			 "day":"30",'+
	    '	      }'+
	    '	   ]'+
	    '	}';
		
	UseAjax(urlStr, jsonStr, document.cookie, rt, '1');
}

function rt(data) {
	var datas = new Array();
	var wi = data.resultlist[0].recordsfirst;
	var afterrecords = data.resultlist[0].recordsafter;
	var sleeprecords = data.resultlist[0].recordssleep;
	var maxsugar = 15;
	var first1=0;
	var after1=0;
	var sleep1=0;
	var first2=0;
	var after2=0;
	var sleep2=0;
	var first3=data.resultlist[0].first;
	var after3=data.resultlist[0].after;
	var sleep3=data.resultlist[0].sleep;
	
	if(wi.length > 0){
	for (var int = 0; int < wi.length; int++) {
		datas[int] = [
				Date.UTC(wi[int].dates.substring(0, 4), wi[int].dates.substring(
						5, 7) - 1, wi[int].dates.substring(8, 10), wi[int].dates
						.substring(11, 13)), Number(wi[int].value) ];
		if(Number(wi[int].value)>maxsugar){
			maxsugar =Number(wi[int].value);
		}
		
		
	}
	first1=Number(wi[0].value);
	first2=Number(wi[wi.length-1].value);
	$('#container1').highcharts(
			{

				title : {
					text : ''
				},
				subtitle : {
					text : getLastDay(now, 0).format("yyyy-MM-dd")+" 到 "+getNextDay(now, maxdate).format("yyyy-MM-dd")+"空腹血糖"
				},
				xAxis : {
					type : 'datetime',
					dateTimeLabelFormats : {
					    second : '%H:%M:%S',
					    minute : '%H:%M',
					    hour : '%H:%M',
					    day : '%m-%d',
					    week : '%m-%d',
					    month : '%m',
					    year : '%Y' 
					},
					min : Date.UTC(now.getFullYear(), now.getMonth(), now
							.getDate() ),
					max : Date.UTC(now.getFullYear(), now.getMonth(), now
							.getDate() + maxdate),
					maxPadding : 5,
					gridLineWidth : 1,
					gridLineDashStyle : 'Dash',// 样式 虚线

				},
				yAxis : {
					labels : {
						step : 2,
						formatter : function() {
							return "" + this.value ;
						}
					},
					tickPixelInterval : 4,
					min : 0, // 固定列最小值
					max : maxsugar,
					title : {
						align: 'high',
		                offset: 10,
		                text: 'mmol/L',
		                rotation: 0,
		                y: -10
					} 
					,
					plotBands : [ {// 标示区域
						from : 4.4,
						to : 10.0,
						color : '#E1F9CE',
						/*label:{
						     text:"4.4-10.0",     //标签的内容
						     align:'left',                //标签的水平位置，水平居左,默认是水平居中center
						     x:-20 ,                        //标签相对于被定位的位置水平偏移的像素，重新定位，水平居左10px
						     verticalAlign: "middle",      
						}*/
					} ],
				},
				tooltip : {
					formatter : function() {
						return 'ceshi';
					}
				},
				plotOptions : {
					line : {
						dataLabels : {
							enabled : true
						},
						enableMouseTracking : false
					}
				},
				credits : {
					enabled : false
				},
				legend : {
					enabled : false
				},
				series : [ {
					pointPadding : 0,// 数据点之间的距离值
					groupPadding : 0,// 分组之间的距离值
					name : null,
					data : datas
				} ]
			});
	}
	
	if(afterrecords.length > 0){
		datas = new Array();
		for (var int = 0; int < afterrecords.length; int++) {
			datas[int] = [
					Date.UTC(afterrecords[int].dates.substring(0, 4), afterrecords[int].dates.substring(
							5, 7) - 1, afterrecords[int].dates.substring(8, 10), afterrecords[int].dates
							.substring(11, 13)), Number(afterrecords[int].value) ];
			if(Number(afterrecords[int].value)>maxsugar){
				maxsugar =Number(afterrecords[int].value);
			}
			
			
		}
		after1=Number(afterrecords[0].value);
		after2=Number(afterrecords[afterrecords.length-1].value);
		$('#container2').highcharts(
				{

					title : {
						text : ''
					},
					subtitle : {
						text : getLastDay(now, 0).format("yyyy-MM-dd")+" 到 "+getNextDay(now, maxdate).format("yyyy-MM-dd")+"餐后血糖"
					},
					xAxis : {
						type : 'datetime',
						dateTimeLabelFormats : {
						    second : '%H:%M:%S',
						    minute : '%H:%M',
						    hour : '%H:%M',
						    day : '%m-%d',
						    week : '%m-%d',
						    month : '%m',
						    year : '%Y' 
						},
						min : Date.UTC(now.getFullYear(), now.getMonth(), now
								.getDate() ),
						max : Date.UTC(now.getFullYear(), now.getMonth(), now
								.getDate() + maxdate),
						maxPadding : 5,
						gridLineWidth : 1,
						gridLineDashStyle : 'Dash',// 样式 虚线

					},
					yAxis : {
						labels : {
							step : 2,
							formatter : function() {
								return "" + this.value ;
							}
						},
						tickPixelInterval : 4,
						min : 0, // 固定列最小值
						max : maxsugar,
						title : {
							align: 'high',
			                offset: 10,
			                text: 'mmol/L',
			                rotation: 0,
			                y: -10
						} 
						,
						plotBands : [ {// 标示区域
							from : 4.4,
							to : 10.0,
							color : '#E1F9CE',
							/*label:{
							     text:"4.4-10.0",     //标签的内容
							     align:'left',                //标签的水平位置，水平居左,默认是水平居中center
							     x:-20 ,                        //标签相对于被定位的位置水平偏移的像素，重新定位，水平居左10px
							     verticalAlign: "middle",      
							}*/
						} ],
					},
					tooltip : {
						formatter : function() {
							return 'ceshi';
						}
					},
					plotOptions : {
						line : {
							dataLabels : {
								enabled : true
							},
							enableMouseTracking : false
						}
					},
					credits : {
						enabled : false
					},
					legend : {
						enabled : false
					},
					series : [ {
						pointPadding : 0,// 数据点之间的距离值
						groupPadding : 0,// 分组之间的距离值
						name : null,
						data : datas
					} ]
				});
		}
	
	//睡前血糖指数
	if(sleeprecords.length > 0){
		datas = new Array();
		for (var int = 0; int < sleeprecords.length; int++) {
			datas[int] = [
					Date.UTC(sleeprecords[int].dates.substring(0, 4), sleeprecords[int].dates.substring(
							5, 7) - 1, sleeprecords[int].dates.substring(8, 10), sleeprecords[int].dates
							.substring(11, 13)), Number(sleeprecords[int].value) ];
			if(Number(sleeprecords[int].value)>maxsugar){
				maxsugar =Number(sleeprecords[int].value);
			}
			
			
		}
		sleep1=Number(sleeprecords[0].value);
		sleep2=Number(sleeprecords[sleeprecords.length-1].value);
		$('#container3').highcharts(
				{

					title : {
						text : ''
					},
					subtitle : {
						text : getLastDay(now, 0).format("yyyy-MM-dd")+" 到 "+getNextDay(now, maxdate).format("yyyy-MM-dd")+"睡前血糖"
					},
					xAxis : {
						type : 'datetime',
						dateTimeLabelFormats : {
						    second : '%H:%M:%S',
						    minute : '%H:%M',
						    hour : '%H:%M',
						    day : '%m-%d',
						    week : '%m-%d',
						    month : '%m',
						    year : '%Y' 
						},
						min : Date.UTC(now.getFullYear(), now.getMonth(), now
								.getDate() ),
						max : Date.UTC(now.getFullYear(), now.getMonth(), now
								.getDate() + maxdate),
						maxPadding : 5,
						gridLineWidth : 1,
						gridLineDashStyle : 'Dash',// 样式 虚线

					},
					yAxis : {
						labels : {
							step : 2,
							formatter : function() {
								return "" + this.value ;
							}
						},
						tickPixelInterval : 4,
						min : 0, // 固定列最小值
						max : maxsugar,
						title : {
							align: 'high',
			                offset: 10,
			                text: 'mmol/L',
			                rotation: 0,
			                y: -10
						} 
						,
						plotBands : [ {// 标示区域
							from : 4.4,
							to : 10.0,
							color : '#E1F9CE',
							/*label:{
							     text:"4.4-10.0",     //标签的内容
							     align:'left',                //标签的水平位置，水平居左,默认是水平居中center
							     x:-20 ,                        //标签相对于被定位的位置水平偏移的像素，重新定位，水平居左10px
							     verticalAlign: "middle",      
							}*/
						} ],
					},
					tooltip : {
						formatter : function() {
							return 'ceshi';
						}
					},
					plotOptions : {
						line : {
							dataLabels : {
								enabled : true
							},
							enableMouseTracking : false
						}
					},
					credits : {
						enabled : false
					},
					legend : {
						enabled : false
					},
					series : [ {
						pointPadding : 0,// 数据点之间的距离值
						groupPadding : 0,// 分组之间的距离值
						name : null,
						data : datas
					} ]
				});
		}
	
	var html = "";
	html = '<table class="bordered"><thead> '
			+ '<tr><th>项目</th><th>最初值</th><th>最末值</th><th>平均值</th> '
			+ '</tr></thead>';
	
		html += ' <tr><td>空腹血糖</td><td>' + first1 + '</td><td>'
				+ first2 + '</td><td>' + first3 + '</td></tr>';
		html += ' <tr><td>餐后血糖</td><td>' + after1 + '</td><td>'
		+ after2 + '</td><td>' + after3 + '</td></tr>';
		html += ' <tr><td>睡前血糖</td><td>' + sleep1 + '</td><td>'
		+ sleep2 + '</td><td>' + sleep3 + '</td></tr>';
	
	$("#container4").html(html);
	$("#container5").html(data.resultlist[0].desc);
	$("#container6").html(data.resultlist[0].subject);
}


